$(function(){
	refresh();
});

function add(){
	addSiteDiag('添加人员',function(){
    	$.post("/selleradd",{
    			phone: $('#v_phone').textbox('getValue'),
    			password: $('#v_password').passwordbox('getValue'),
    			name: $('#v_name').textbox('getValue'),
    			isadmin: $('#v_isadmin').switchbutton('options').checked,
    			isassistant: $('#v_isassistant').switchbutton('options').checked
    		}, function(data) {
    			$('#dd').dialog('close');
    			if(data=='Success'){
    				$.messager.alert('','添加完成!','');
    				refresh();
    			}else $.messager.alert('','添加失败!','error');
		});
    });
}

function edit(){
	var row = $('#dg').datagrid('getSelected');
    if (row){
    	addSiteDiag('修改信息',function(){
        	$.post("/sellermod",{
        			phone: $('#v_phone').textbox('getValue'),
        			password: $('#v_password').passwordbox('getValue'),
        			name: $('#v_name').textbox('getValue'),
        			isadmin: $('#v_isadmin').switchbutton('options').checked,
        			isassistant: $('#v_isassistant').switchbutton('options').checked
        		}, function(data) {
        			$('#dd').dialog('close');
        			if(data=='Success'){
        				$.messager.alert('','添加完成!','');
        				refresh();
        			}else $.messager.alert('','添加失败!','error');
    		});
        });
    	$('#v_phone').textbox('setValue',row.phone).textbox('disable');
    	$('#v_password').passwordbox('disable');
    	$('#v_name').textbox('setValue',row.name).textbox('disable');
    	console.log(row.isadmin,row.isassistant);
    	if(row.isadmin) $('#v_isadmin').switchbutton('check');
    	else $('#v_isadmin').switchbutton('uncheck');
    	if(row.isassistant) $('#v_isassistant').switchbutton('check');
    	else $('#v_isassistant').switchbutton('uncheck');
    }else{
		$.messager.alert('','未选中需编辑的人!','error');
	}
}

function remove(){
	$.messager.confirm('删除人员数据', '删除 人员数据会同时删除其相关销售信息等数据，请确认是否删除?', function(r){
		if (r){
        	var row = $('#dg').datagrid('getSelected');
            if (row){
            	$.post("/sellerdel",{ phone: row.phone }, function() {
            		$.messager.alert('', '['+row.name+"("+row.phone+")] 人员数据已删除。");
            		refresh();
        		});
            }
        }
    });
}

function addSiteDiag(title,handlerfunction){
	$('#dd').dialog({
	    title: title,
		closed: false,
	    cache: false,
	    modal: true,
	    buttons: [
	              {text:'保存',iconCls:'icon-ok',handler:handlerfunction},
	              {text:'取消',iconCls:'icon-cancel',handler: function(){$('#dd').dialog('close');} }
	              ]
	});
	$('#v_phone').textbox('setValue','').textbox('enable');
	$('#v_password').passwordbox('setValue','').passwordbox('enable');
	$('#v_name').textbox('setValue','').textbox('enable');
}

function search(){
	
}

function refresh(){
	loadUrlDataGrid('dg','/sellerall',{},
			[[]],
			[[{field:'phone',title:'手机号码',align:'center',sortable:true},
			  {field:'name',title:'姓名',align:'center',sortable:true},
			  {field:'isassistant',title:'是否营业员',width:300,align:'center',sortable:true,
				  formatter: function(value,row,index){
					  if(row.isassistant) return "是";
					  else return "否";
				  }},
			  {field:'isadmin',title:'是否管理员',align:'center',sortable:true,
				  formatter: function(value,row,index){
					  if(row.isadmin) return "是";
					  else return "否";
				  }}
		    ]],
			'#tb');
}

function loadUrlDataGrid(div,url,queryParams,frozenColumns,columns,toolbar){
	$('#'+div).datagrid({
	    url:url,
	    queryParams:queryParams,
	    frozenColumns:frozenColumns,
	    columns:columns,
	    singleSelect:true,
	    collapsible:true,
	    rownumbers:true,
        multiSort:true,
        pagination:false,
	    toolbar: toolbar
	});
}